#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author: Florian Dorn <florian.dorn@gmail.com>
# @package: agenda
# @license: GNU GPLv2
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import gtk
import pango
import gobject


from gettext import gettext as _

class ProgressWidget (gtk.HBox):
	__gtype_name__ = 'ProgressWidget'
	__gsignals__ = {
		
		'pixbuf-changed'	     : (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE,
					[ gobject.TYPE_PYOBJECT ])
		}
	"""
	Widget to display progress information	
	"""
	def get_current_pixbuf(self):
		"""
		returns the current pixbuf
		"""
		return self.image.get_pixbuf()
	def __init__(self):
		gtk.HBox.__init__(self)
		self.set_spacing(2)
		
		self.text = gtk.Label()
		self.text.set_ellipsize(pango.ELLIPSIZE_END)
		self.text.set_alignment(0,0.5)


		
		self.image = gtk.Image()
		
		self.__size = 22
		self.__theme = gtk.icon_theme_get_default()
		self.theme_changed_cb()
		self.image.set_size_request(self.__size, self.__size)

		self.pack_start(self.image,False,False)
		self.pack_start(self.text,True,True)
		
		self.show_all()
		self.reset()
		self.ensure_style()
		self._style_source = self.connect ("notify::style", self.theme_changed_cb) # gtk theme changes
		
	def theme_changed_cb(self, obj=None, prop=None):
		self.__theme.rescan_if_needed()
		self.__pixbuf = self.__theme.load_icon(
				'process-working',
				self.__size,
				gtk.ICON_LOOKUP_GENERIC_FALLBACK)
		self.__hframes = self.__pixbuf.get_width() / self.__size
		self.__vframes = self.__pixbuf.get_height() / self.__size
		self.__hpos = 0
		self.__vpos = 0
		
	def set_text(self,text):
		self.text.set_markup('%s' % text)
		
	def reset(self):
		self.__busy_id = None
		self.__busy = False
		self.set_text(" ")

	def set_from_pixbuf(self,pixbuf):
		self.image.set_from_pixbuf(pixbuf)
		self.emit("pixbuf-changed",self.image.get_pixbuf())
	def __spinner_timeout(self):
		"""Timeout callback that spins the spinner."""
		# This is wrong
		if not self.__busy:
			self.set_from_pixbuf(None)
			return False

		# Skip the first (resting) frame to avoid flicker
		if self.__hpos == 0 and self.__vpos == 0:
			self.__hpos = 1
			if self.__hpos == self.__hframes:
				self.__hpos = 0
				self.__vpos += 1
			if self.__vpos >= self.__vframes:
				self.__vpos = 0		
		x = self.__size * self.__hpos
		y = self.__size * self.__vpos
		pixbuf = self.__pixbuf.subpixbuf(x, y, self.__size, self.__size)

		self.set_from_pixbuf(pixbuf)
		
		self.__hpos += 1
		if self.__hpos == self.__hframes:
			self.__hpos = 0
			self.__vpos += 1
		if self.__vpos >= self.__vframes:
			self.__vpos = 0
		
		return True

	def set_busy(self, busy=True):
		if self.__busy and busy:
			return
		if not busy:
			self.set_from_pixbuf(None)
		
		if self.__busy_id:
			gobject.source_remove(self.__busy_id)

		self.__busy = busy
		self.__busy_id = gobject.timeout_add(100, self.__spinner_timeout)
		
